package useless;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;

public class Axis3DPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();

        // 计算中心点
        int centerX = width / 2;
        int centerY = height / 2;

        // 坐标轴终点（简单的 3D 透视效果）
        int axisLength = 100;

        // X 轴（红色）
        drawLine(g2d, centerX, centerY, centerX + axisLength, centerY, Color.RED, "X");
        // Y 轴（绿色）
        drawLine(g2d, centerX, centerY, centerX, centerY - axisLength, Color.GREEN, "Y");
        // Z 轴（蓝色，向右下方倾斜）
        drawLine(g2d, centerX, centerY, centerX - axisLength / 2, centerY + axisLength / 2, Color.BLUE, "Z");
    }

    private void drawLine(Graphics2D g2d, int x1, int y1, int x2, int y2, Color color, String label) {
        g2d.setColor(color);
        g2d.draw(new Line2D.Double(x1, y1, x2, y2));
        g2d.drawString(label, x2 + 5, y2 - 5);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("3D Coordinate Axes");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.add(new Axis3DPanel());
            frame.setVisible(true);
        });
    }
}
